<template>
  <span class="content-each">
    <el-popover placement="bottom" v-model="assetInfoVisible">
      <div>资产名称：{{ results.assetName }}</div>
      <div>资产类型：{{ results.assetType }}</div>
      <div>业务系统：{{ results.businessType }}</div>
      <template slot="reference">
        <span
          class="link"
          @mouseenter="getAssetInfo"
          @mouseleave="assetInfoHidden"
          >{{ typeValue }}
        </span>
      </template>
    </el-popover>
  </span>
</template>

<script>
import { getAssetInfo } from "@/api/alarmCenter/index";

export default {
  name: "Popover",
  props: {
    width: {
      type: Number,
      default: 180,
    },

    label: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "",
    },
    typeValue: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      visible: false,
      assetInfoVisible: false,
      results: {
        assetName: "",
        assetType: "",
        businessType: "",
      },
      isOver: false, //鼠标是否移入
      timer: null,
      startTime: null, //鼠标移入时间
    };
  },

  methods: {
    getAssetInfo() {
      this.isOver = true;
      this.startTime = new Date().getTime();
      this.timer = setTimeout(() => {
        getAssetInfo({ manageIp: this.typeValue }).then((res) => {
          this.assetInfoVisible = this.isOver;
          if (res && res.code === 200) {
            if (res.data) {
              this.results = res.data;
            } else {
              this.results = {
                assetName: "",
                assetType: "",
                businessType: "",
              };
            }
          } else {
            this.results = {
              assetName: "",
              assetType: "",
              businessType: "",
            };
          }
        });
      }, 500);
    },
    assetInfoHidden() {
      this.assetInfoVisible = false;
      this.isOver = false;
      let endTime = new Date().getTime();
      if (endTime - this.startTime < 500) {
        //防止鼠标来回移动触发事件
        clearTimeout(this.timer);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.content-each {
  cursor: pointer;
}

.first-content {
  padding: 0px 10px 0px 0px;
}

.last-content {
  border-right: 0;
}

.link {
  color: var(--theme-color);
}
</style>
